package com.stm.quartz.task;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * 定时任务调度测试
 * 
 * @author admin
 */
@Slf4j
@Component("ryTask")
public class RyTask
{
	/**
	 * 
	 * @Description 定时重启 每隔3天凌晨2点重启
	 * @CreateTime 2019年10月23日 下午2:37:09
	 * @CreateBy 林龙
	 * @param params
	 * @throws IOException
	 * @throws InterruptedException
	 */
    public void ryParams(String params) throws IOException, InterruptedException
    {
    	String osName = System.getProperty("os.name", "");
    	if (osName.startsWith("Windows")) {
    		
    	}else{
    		Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","reboot"});
    	}
    	
    	
    	 /*  log.info("验证视频软件是否工作,开始");
        try {
            URL url = new URL("http://"+params+"/api/v1/Search?type=record&start=2018-0307T120101%2b08&end=3019-05-18T130101%2b08&token=token1&session=");
            InputStream in =url.openStream();
            InputStreamReader isr = new InputStreamReader(in);
            BufferedReader bufr = new BufferedReader(isr);
            log.info("验证视频软件是否工作，url地址："+url);
            bufr.close();
            isr.close();
            in.close();
        } catch (Exception e) {
        	 log.info("重启录制视频软件：restart：{}"+params);
        	restart(params); //执行视频重启
        	log.info("重启录制视频软件成功");
        }*/
    	
    }
    
    /**
     * 
     * @Description 重启录制视频软件
     * @CreateTime 2019年7月5日 上午4:49:48
     * @CreateBy 林龙
     * @param params
     * @throws IOException
     * @throws InterruptedException
     */
    public void restart(String params) throws IOException, InterruptedException
    {
    	String osName = System.getProperty("os.name", "");
    	if (osName.startsWith("Windows")) {
    		
    		Runtime runtime = Runtime.getRuntime();
    		Process process = runtime.exec("c:/nircmd.exe elevate net stop H5Stream");
    		process.waitFor();
    		
    		Runtime runtime1 = Runtime.getRuntime();
    		Process process1 = runtime1.exec("c:/nircmd.exe elevate net start H5Stream");
    		process1.waitFor();
    		
    	}else{
    		
    		Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","systemctl restart h5ss.service"});
    	}
    	
    	log.info("启动录制视开始");
    	URL url = new URL("http://"+params+"/api/v1/Record?token=token1&duration=300&session=");
    	InputStream in =url.openStream();
    	InputStreamReader isr = new InputStreamReader(in);
    	BufferedReader bufr = new BufferedReader(isr);
    	bufr.close();
    	isr.close();
    	in.close();
    	log.info("启动录制视频成功，url地址：{}"+url);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }
}
